# -*- coding: utf-8 -*-
# Description: 切面
# Created: dingwenjing 2022/11/29
import json
from flask import request, make_response
from datetime import datetime
from app.main import blueprint
from flask import current_app, g


# 每个请求和响应的日志
@blueprint.before_request
def before_request():
    g.start_time = datetime.now()
    g.is_except = False
    g.remark_num_set = None
    current_app.logger.info('===========================start=============================')
    current_app.logger.info('before_request')
    current_app.logger.info('method is ' + request.method)
    current_app.logger.info('url is ' + request.url)
    current_app.logger.info('body is ' + json.dumps(request.json, ensure_ascii=False))
    current_app.logger.info('ip is ' + request.remote_addr)


@blueprint.after_request
def after_request(response):
    current_app.logger.info("after_request")
    current_app.logger.info("body is " + json.dumps(response.json, ensure_ascii=False))
    current_app.logger.info("execution time:" + str(int((datetime.now() - g.start_time).total_seconds() * 1000)))
    current_app.logger.info('===========================end===============================')
    # if not hasattr(g, "profiler"):
    #     return response
    # g.profiler.stop()
    # output_html = g.profiler.output_html()
    # return make_response(output_html)
    return response
